package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.guigu.mapper.ConsumerMapper;
import com.guigu.mapper.MerchantMapper;
import com.guigu.mapper.OrdersMapper;
import com.guigu.pojo.*;
import com.guigu.service.OrderDetailsService;
import com.guigu.service.OrdersService;
import com.guigu.service.PayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private OrderDetailsService orderDetailsService;

    @Autowired
    private PayService payService;

    @Override
    public Page<Orders> getAllOrder(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }

        Page<Orders> page = new Page<>(currentPage, pageSize);
        List<Orders> orders = ordersMapper.getOrders(map);

        PageHelper.startPage(currentPage, pageSize);
        List<Orders> orders2 = ordersMapper.getOrders(map);
        Integer count = ordersMapper.selectCount(null);
        System.out.println(orders);
        if (orders != null) {
            page.setTotal(count);
            page.setRecords(orders2);
            return page;
        }
        return null;
    }



    @Override
    public Orders selectByOrderNo(String orderNo) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderNo);
        if (ordersMapper.selectOne(queryWrapper)!=null){
            return ordersMapper.selectOne(queryWrapper);
        }
        return null;
    }

    @Override
    public Page<Orders> getOrderByMerchantIdOrConsumerId(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }

        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }
        Page<Orders> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        Object merchantIdObj = map.get("merchantId");
        if (merchantIdObj != null) {
            if (merchantIdObj instanceof String && StringUtils.isNotBlank((String) merchantIdObj)) {
                queryWrapper.eq("merchant_id", (String) merchantIdObj);
            } else if (merchantIdObj instanceof Integer) {
                queryWrapper.eq("merchant_id", merchantIdObj.toString());
            }
            queryWrapper.eq("pay_tag","K002-3");
        }

        Object consumerIdObj = map.get("consumerId");
        if (consumerIdObj != null) {
            if (consumerIdObj instanceof String && StringUtils.isNotBlank((String) consumerIdObj)) {
                queryWrapper.eq("consumer_id", (String) consumerIdObj);
            } else if (consumerIdObj instanceof Integer) {
                queryWrapper.eq("consumer_id", consumerIdObj.toString());
            }
        }

        if (map.get("orderNumber") != null && StringUtils.isNotBlank((String) map.get("orderNumber"))) {
            queryWrapper.like("order_number",map.get("orderNumber"));
        }
        ArrayList<Object> arr = (ArrayList<Object>) map.get("createDate");
        if (map.get("createDate") != null && arr.size()>0){
            queryWrapper.between("create_date",arr.get(0),arr.get(1));
        }
        queryWrapper.eq("status","C001-1");
        Page<Orders> ordersPage = ordersMapper.selectPage(page, queryWrapper);
        if (ordersPage.getTotal()>0){
            ordersPage.getRecords().forEach(item->{
                Integer consumerId = item.getConsumerId();
                Consumer consumer = consumerMapper.selectById(consumerId);
                Integer merchantId = item.getMerchantId();
                Merchant merchant = merchantMapper.selectById(merchantId);

                List<OrderDetails> orderDetailsByOrderId = orderDetailsService.getOrderDetailsByOrderId(item.getOrderId());
                item.setOrderDetails(orderDetailsByOrderId);

                item.setConsumer(consumer);
                item.setMerchant(merchant);
            });
        }

        return ordersPage;
    }

    @Override
    public Integer getMerchantOrderCountByMerchantId(Integer merchantId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id",merchantId);
        queryWrapper.eq("pay_tag","K002-3");
        if (ordersMapper.selectCount(queryWrapper)>0){
            return ordersMapper.selectCount(queryWrapper);
        }
        return 0;
    }

    @Override
    public Boolean cancelOrder(String orderNumber) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderNumber);
        if (ordersMapper.selectOne(queryWrapper)!=null){
            Orders orders = ordersMapper.selectOne(queryWrapper);
            orders.setStatus("C001-2");
            ordersMapper.updateById(orders);
            return true;
        }
        return false;
    }

    @Override
    public Page<Orders> getAllocateOrder(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }

        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }
        Page<Orders> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        if (map.get("orderNumber") != null && StringUtils.isNotBlank((String) map.get("orderNumber"))) {
            queryWrapper.like("order_number",map.get("orderNumber"));
        }
        if (map.get("data1") != null && map.get("data2") != null){
            queryWrapper.between("create_date",map.get("data1"),map.get("data2"));
        }
        queryWrapper.eq("status","C001-1");
        queryWrapper.eq("pay_tag","K002-3");
        queryWrapper.eq("convey_tag","P001-1");
        Page<Orders> ordersPage = ordersMapper.selectPage(page, queryWrapper);
        if (ordersPage.getTotal()>0){
            ordersPage.getRecords().forEach(item->{
                Integer consumerId = item.getConsumerId();
                Consumer consumer = consumerMapper.selectById(consumerId);
                item.setConsumer(consumer);
                Integer merchantId = item.getMerchantId();
                Merchant merchant = merchantMapper.selectById(merchantId);
                item.setMerchant(merchant);
                Pay pay = payService.getPayByOrderList(item.getOrderNumber());
                item.setPay(pay);

                List<OrderDetails> orderDetailsByOrderId = orderDetailsService.getOrderDetailsByOrderId(item.getOrderId());
                item.setOrderDetails(orderDetailsByOrderId);
            });
        }
        return ordersPage;
    }


}
